import {
	defineStore
} from 'pinia'
import {
	apiGetAccount,
	apiLogOut
} from '@/api/apis'

export const useUserStore = defineStore({
	id: 'user',
	state: () => {
		return {
			id: '',
			nickname: '',
			isLog: false
		}
	},
	actions: {
		setAccount(data) {
			this.id = data.id
			this.nickname = data.nickname
			this.isLog = true
		},
		async verifyLog(data) {
			const token = uni.getStorageSync('token')
			if (token) {
				await  apiGetAccount().then(res => {
					if (res.code != 200) {
						uni.clearStorage()
						return false
					} else {
						this.setAccount(res.data)
					}
				})
				return true
			}
			return false
		},
		logOut() {
			apiLogOut().then(res => {
				if (res.code == 200) {
					uni.showToast({
						title: "退出成功",
						icon: "none"
					})
					uni.redirectTo({
						url: "/pages/login/login"
					})
					uni.clearStorage()
				} else {
					uni.showToast({
						title: "退出失败",
						icon: "none"
					})
				}
			})
		}
	}
})